/**
*      author: Y_Yao
*      created: 2025-04-02 16:06:02
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 3e4 + 7;
int n,q;
vector<pair<int,int>> G[N];
int dp[100+7][N];

void dfs(int fa,int u)
{
    int left = 0,right = 0,lw = 0,rw = 0;
    int w = 0;
    for(int i = 0;i < G[u].size();i++)
    {
        int son = G[u][i].first;
        if(son == fa) continue;
        w = G[u][i].second;
        if(!left)
        {
            left = son;
            lw = w;
        }
        else
        {
            right = son;
            rw = w;
        }
        dfs(u,son);
    }
    for(int i = 1;i <= q;i++)
    {
        for(int l = 0,r = i;l <= i;l++,r--)
        {
            if(l > 0 && r > 0)//左右都选了
            {
                dp[u][i] = max(dp[u][i],lw + dp[left][l-1] + rw + dp[right][r-1]);
            }
            else if(l == 0)//左边不选，全选右边
            {
                dp[u][i] = max(dp[u][i],dp[right][r-1] + rw);
            }
            else
            {
                dp[u][i] = max(dp[u][i],dp[left][l-1] + lw);
            }
        }
    }
}

void solve()
{
    cin >> n >> q;
    for(int i = 1;i <= n - 1;i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        G[u].push_back({v,w});
        G[v].push_back({u,w});
    }
    dfs(0,1);
    cout << dp[1][q] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}